/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "plane.h"

#include "math/ray.h"
#include "material/material.h"

#include <cmath>
#include <limits>

namespace Sirat {

namespace Object {

Plane::Plane(Material::Object_Material* _object_material,const Math::Vector& _point,const Math::Vector& _normal) : Object(_object_material), point(_point) , normal(_normal) {
	normal = _normal.normalize();
}

Plane::Plane() { }

Plane::Plane(const Plane& _plane) : Object(_plane) {
	point = _plane.point;
	normal = _plane.normal;
}

Plane::~Plane() { 
}

tuple<double,Math::Vector,Object*> Plane::intersection(const Math::Ray& _ray) {
	std::numeric_limits<double> double_limit;
	const double infinity = double_limit.infinity();
	
	const double n_dot_d = normal.dot_product(_ray.get_direction());
	Math::Vector intersection_point;
	
	if(n_dot_d < 0.0) {
		const double distance = (normal.dot_product(point.sub_vect(_ray.get_origin()))) / (n_dot_d);
		
		if(distance >= 0.0) { 
			intersection_point = _ray.get_origin() + _ray.get_direction()*distance;
			return make_tuple(distance,intersection_point,this);
		}
	}
	return make_tuple(infinity,intersection_point,this);
	
}

Math::Vector Plane::get_normal(const Math::Vector&) const { 
	return normal; 
}

Math::Vector Plane::get_point() const { 
	return point; 
}

void Plane::set_normal(const Math::Vector& _normal) {
	normal = _normal.normalize();
}

void Plane::set_point(const Math::Vector& _point) {
	point = _point;
}

Plane& Plane::operator=(const Plane& _plane) {
	Object::operator=(_plane);
	point = _plane.point;
	normal = _plane.normal;
	return *this;
}

}

}
